//
//  SISSContractCheckCell.m
//  SISSAPP
//
//  Created by 优房网 on 2017/8/25.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSContractCheckCell.h"

@interface SISSContractCheckCell ()<UITextFieldDelegate>

/// checkButton
@property (weak, nonatomic) UIButton *checkButton;
/// Label
@property (weak, nonatomic) UILabel *label;
/// textField
@property (weak, nonatomic) UITextField *textField;
/* bottomLine */
@property (weak, nonatomic) UIView *bottomLine;

@end
@implementation SISSContractCheckCell

- (void)setContractModel:(SISSContractModel *)contractModel {
    [super setContractModel:contractModel];
    
    
//    self.label.text = contractModel.leftStr;
    [self.checkButton setTitle:contractModel.leftStr forState:UIControlStateNormal];
    self.textField.placeholder = contractModel.placeHolder;
    self.isShowTextField = contractModel.isShowFillIn;
    self.isDisabledTextField = contractModel.isDisabled;
    self.checkButton.selected = contractModel.isChecked;
    self.textField.userInteractionEnabled = contractModel.isChecked;
    self.textField.text = contractModel.rightStr;
    
}

- (void)setIsShowTextField:(BOOL)isShowTextField {
    _isShowTextField = isShowTextField;
    self.textField.hidden = !isShowTextField;
}

- (void)setIsDisabledTextField:(BOOL)isDisabledTextField {
    _isDisabledTextField = isDisabledTextField;
    self.textField.userInteractionEnabled = !isDisabledTextField;
}

#pragma mark - 点击事件
- (void)checkButtonClickAction:(UIButton *)button {
    
    button.selected = !button.selected;
    self.textField.userInteractionEnabled = button.isSelected;
    self.contractModel.isChecked = button.isSelected;
}


#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
    self.contractModel.rightStr = textField.text;
}


#pragma mark - UI
- (void)initUI {
    [super initUI];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"checkSelected"] forState:UIControlStateSelected];
    [button setTitle:@"checkTitle" forState:UIControlStateNormal];
    [button setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [button addTarget:self action:@selector(checkButtonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:button];
    self.checkButton = button;
    
    UILabel *label = [[UILabel alloc] init];
    label.numberOfLines = 0;
    label.textColor = MAIN_TEXT_COLOR;
    label.font = [UIFont systemFontOfSize:16];
    label.textAlignment = NSTextAlignmentRight;
    [self.contentView addSubview:label];
    self.label = label;
    
    UITextField *textField = [[UITextField alloc] init];
    textField.delegate = self;
    textField.textColor = MAIN_TEXT_COLOR;
    textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@" " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:SECONDARY_TEXT_COLOR}];
    textField.hidden = !self.isShowTextField;
    textField.userInteractionEnabled = !self.isDisabledTextField;
    textField.placeholder = self.contractModel.placeHolder;
    [self.contentView addSubview:textField];
    self.textField = textField;
    
    UIView *bottomLine = [[UIView alloc] init];
    bottomLine.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.contentView addSubview:bottomLine];
    self.bottomLine = bottomLine;

}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    WeakSelf
    CGFloat margin = 8.0;
    
    [self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
    }];
    
//    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(weakSelf.checkButton.mas_right).offset(margin);
//        make.top.equalTo(weakSelf.contentView).offset(1.5 * margin);
//    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_centerX);
        make.centerY.equalTo(weakSelf.contentView);
    }];
    
    [self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.checkButton.mas_bottom).offset(1.5 * margin);
        make.left.right.bottom.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(1);
    }];
}



@end
